<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://www.joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		GNU GPL v3
*/

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');
JLoader::register('DaniProHelper', JPATH_COMPONENT.'/helpers/danipro.php');
class DaniProControllerMedia extends JControllerForm
{
	/**
	 * @var    string  The prefix to use with controller messages.
	 * @since  1.6
	 */
	protected $text_prefix = 'COM_DANIPRO_MEDIA';

	/**
	 * Method override to check if you can add a new record.
	 *
	 * @param   array  $data  An array of input data.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	
	 
	protected function allowAdd($data = array())
	{
		// Initialise variables.
		$user		= JFactory::getUser();
		if ($allow === null)
		{
			// In the absence of better information, revert to the component permissions.
			return parent::allowAdd($data);
		}
		else
		{
			return $allow;
		}
	}

	public function save()
	{
		require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/class.image.php';
		//upload
		$path_thumb = JPATH_ROOT.'/components/com_danipro/images/product/thumb/';	
		$path_large = JPATH_ROOT.'/components/com_danipro//images/product/large/';	
		$path_temp = JPATH_ROOT.'/components/com_danipro/images/temp/';	
		$file = $_FILES['jform'];
		if(move_uploaded_file($file['tmp_name']['image'],$path_temp.$file['name']['image']))
		{
			$info = pathinfo($path_temp.$file['name']['image']);
			$newname = DaniProHelper::getImageName().'.'.$info['extension'];
			//copy to thumb folder and resize
			copy($path_temp.$file['name']['image'],$path_thumb.$newname);
			$resizethum = new img_opt();
			$resizethum->image_path($path_thumb.$newname);
			$resizethum->max_width(DANIPRO_PRO_THUMB_WIDTH);
			$resizethum->max_height(DANIPRO_PRO_THUMB_HEIGHT);
			$resizethum->image_resize();
			//copy to large folder and resize
			copy($path_temp.$file['name']['image'],$path_large.$newname);
			$resizelarge = new img_opt();
			$resizelarge->image_path($path_large.$newname);
			$resizelarge->max_width(DANIPRO_PRO_LARGE_WIDTH);
			$resizelarge->max_height(DANIPRO_PRO_LARGE_HEIGHT);
			$resizelarge->image_resize();
			unlink($path_temp.$file['name']['image']);
			$_POST['jform']['image'] = $newname;			
		}
		parent::save();
	}
	

}
